package com.linxiao.hrcrm.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.TFEnums;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linxiao.hrcrm.mapper.CrmBlacklistMapper;
import com.linxiao.hrcrm.domain.CrmBlacklist;
import com.linxiao.hrcrm.service.ICrmBlacklistService;

/**
 * 客户黑名单Service业务层处理
 *
 * @author zhoudong
 * @date 2024-03-20
 */
@Service
public class CrmBlacklistServiceImpl implements ICrmBlacklistService {
    @Autowired
    private CrmBlacklistMapper crmBlacklistMapper;

    /**
     * 查询客户黑名单
     *
     * @param id 客户黑名单主键
     * @return 客户黑名单
     */
    @Override
    public CrmBlacklist selectCrmBlacklistById(Long id) {
        return crmBlacklistMapper.selectCrmBlacklistById(id);
    }

    /**
     * 查询客户黑名单列表
     *
     * @param crmBlacklist 客户黑名单
     * @return 客户黑名单
     */
    @Override
    public List<CrmBlacklist> selectCrmBlacklistList(CrmBlacklist crmBlacklist) {
        return crmBlacklistMapper.selectCrmBlacklistList(crmBlacklist);
    }

    /**
     * 新增客户黑名单
     *
     * @param crmBlacklist 客户黑名单
     * @return 结果
     */
    @Override
    public int insertCrmBlacklist(CrmBlacklist crmBlacklist) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        crmBlacklist.setCreateBy(loginUser.getUsername());
        crmBlacklist.setAvailable(TFEnums.T.name());
        crmBlacklist.setCreateTime(DateUtils.getNowDate());
        return crmBlacklistMapper.insertCrmBlacklist(crmBlacklist);
    }

    /**
     * 修改客户黑名单
     *
     * @param crmBlacklist 客户黑名单
     * @return 结果
     */
    @Override
    public int updateCrmBlacklist(CrmBlacklist crmBlacklist) {
        crmBlacklist.setUpdateTime(DateUtils.getNowDate());
        return crmBlacklistMapper.updateCrmBlacklist(crmBlacklist);
    }

    /**
     * 批量删除客户黑名单
     *
     * @param ids 需要删除的客户黑名单主键
     * @return 结果
     */
    @Override
    public int deleteCrmBlacklistByIds(Long[] ids) {
        return crmBlacklistMapper.deleteCrmBlacklistByIds(ids);
    }

    /**
     * 删除客户黑名单信息
     *
     * @param id 客户黑名单主键
     * @return 结果
     */
    @Override
    public int deleteCrmBlacklistById(Long id) {
        return crmBlacklistMapper.deleteCrmBlacklistById(id);
    }

    /**
     * 根据商户ID和客户ID查询是否在黑名单
     * @param mchId
     * @param customerId
     * @return
     */
    @Override
    public CrmBlacklist selectCrmBlacklistByMchIdAndCustomerId(Long mchId, Long customerId) {
        CrmBlacklist query = new CrmBlacklist();
        query.setMchId(mchId);
        query.setCustomerId(customerId);
        query.setAvailable(TFEnums.T.name());
        List<CrmBlacklist> crmBlacklists = crmBlacklistMapper.selectCrmBlacklistList(query);
        if (CollUtil.isNotEmpty(crmBlacklists)) {
            return crmBlacklists.get(0);
        }
        return null;
    }
}
